﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Resources;

using FinalProject.BL;
using FinalProject.DAL.Model;

namespace FinalProject.UI.Controls
{
    public class CustomerGrid : DataGrid
    {
        public DataGridTextColumn colID, colCode, colName, colTaxCode, colIBAN, colAddress;
        public CustomerGrid()
            : base()
        {
            // Insert code required on object creation below this point.
            this.InitializeColumns();
            this.IsReadOnly = true;
            this.CanSelectMultipleItems = false;
            this.CanUserAddRows = false;
            this.CanUserDeleteRows = false;
            this.SelectionMode = DataGridSelectionMode.Single;
            this.SelectionUnit = DataGridSelectionUnit.FullRow;
        }

        private void InitializeColumns()
        {
            this.colID = new DataGridTextColumn()
            {
                Header = "Id",
                Visibility = System.Windows.Visibility.Hidden
            };

            this.colCode = new DataGridTextColumn()
            {
                Header = "Code",
                Binding = new Binding("Code"),
                Width = 100,
            };
            this.Columns.Add(colCode);

            this.colName = new DataGridTextColumn()
            {
                Header = "Name",
                Binding = new Binding("Name"),
                Width = 300,
            };
            this.Columns.Add(this.colName);

            this.colTaxCode = new DataGridTextColumn()
            {
                Header = "Tax code",
                Binding = new Binding("TaxCode"),
                Width = 150
            };
            this.Columns.Add(this.colTaxCode);

            this.colIBAN = new DataGridTextColumn()
            {
                Header = "Bank account",
                Binding = new Binding("IBAN"),
                Width = 100
            };
            this.Columns.Add(this.colIBAN);

            this.colAddress = new DataGridTextColumn()
            {
                Header = "Address",
                Binding = new Binding("Address"),
                Width = 250
            };
            this.Columns.Add(colAddress);
        }

        public void RefreshData()
        {
            this.ItemsSource = BL.CustomerBL.GetAll();
            if (!this.Items.IsNullOrEmpty())
            {
                this.SelectedIndex = 0;
            }
        }

        public Guid GetSelectedId()
        {
            var selectedCells = this.SelectedCells;
            Guid selectedId = Guid.Empty;
            if (!selectedCells.IsNullOrEmpty())
            {
                selectedId = selectedCells[0].Item.ToGuid();
            }
            return selectedId;
        }

        public Customer GetSelectedValue()
        {
            Customer result = this.SelectedValue as Customer;
            return result;
        }
    }
}